<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>
<head>
	<meta charset='utf-8' /> 
	<title>台账记录</title>
	<style>
		.cursers,#fpage > a{cursor:pointer}
	</style>
</head>
<body style='background:#ddd;padding:0px;margin:0px;padding-left:10px'>
	<div style='border:0px solid;font:normal normal 12px/30px normal'>
		<div style='float:left'>　
			<span style='color:blue' class='cursers' onclick=_jumpDesign()>台账设计</span>
		</div>
		<div style='float:right;padding-right:20px'>　
			<span style='color:blue' class='cursers' onclick='_backGo()'>返回</span>
		</div>
		<div style='clear:both'></div>
	</div>

	<div>
		<h1 style='text-align:center' id='ledgerName'><<台账名称>></h1>		
	</div>


	<!--分类-->
	<div style='text-align:center;width:80%;margin:10px auto;border:0px solid'>
		<div style='float:left;display:none' id='selectArea'>
			<span style='font-size:12px' id='selectName'>类别：</span>
			<select id="selectSel">
				<option value="all">全部</option>
			</select>
		</div>
	
		<div style='float:right;display:none' id='searchArea'>
			<select id="searchSel">
				<option value="">a</option>
				<option value="">b</option>
				<option value="">c</option>
			</select>
			<input type="text" id='searchInp' />
			<button id='searchBtn'>搜索</button>
		</div>

		<div style='clear:both'></div>
	</div>

	<!--台账记录列表-->
	<table style='text-align:center;width:80%;border:1px solid' align='center'  border=1>
		<thead id='thead'>
		</thead>
		<tbody id='tbody'>
			<tr>
				<td><h2>请先设计台账</h2></td>
			</tr>
		</tbody>
	</table>

	<div style='width:80%;margin:20px auto;display:none' id='createNewInfoArea'>
		<button onclick='_createNewInfo()'>添加新记录</button>
	</div>


	<div id='fpage' style='margin:30px auto;text-align:center;display:none'></div>

	<div id='editArea' style='display:none;position:fixed;top:0px;left:0px;width:100%;height:100%;background:#ddd;overflow-y:scroll'>
		<div style='width:100%;border:0px solid;position:absolute;top:30px'>
			<div style='width:450px;border:0px solid;margin:0 auto' id='a1'>
				<h1 style='text-align:center'  id='addInfoTitle'>添加新记录</h1>
				<form action="../interface/before_interface.php" method="post" id='f1'>
					<div id='addNewInfoArea'>
						<div style='margin:10px auto;border:0px solid blue'>
							<div style='float:left;width:25%;border:0px solid;text-align:right'>aa</div>　
							<div style='float:right;width:70%;border:0px solid'><input type="text" style='width:200px' /></div>
						</div>
						<div style='margin:10px auto;border:0px solid blue'>
							<div style='float:left;width:25%;border:0px solid;text-align:right'>aa</div>　
							<div style='float:right;width:70%;border:0px solid'><input type="text" style='width:200px' /></div>
						</div>
						<div style='margin:10px auto;border:0px solid blue'>
							<div style='float:left;width:25%;border:0px solid;text-align:right'>aa</div>　
							<div style='float:right;width:70%;border:0px solid'><textarea name="" id="" style='width:200px;height:150px;resize:none'></textarea></div>
						</div>

					</div>
					
					<div style='clear:both;text-align:center;padding:20px 0'>
						<button type='button' id='addNewInfoBtn'>提交</button>
						<button type='button' onclick='_editBack()'>返回</button>
					</div>
				</form>
			</div>
		</div>		
	</div>

</body>
</html>
<script src='../public/jquery1.1.js'></script>
<script src='../public/fun.js'></script>
<script>
_checkExit();
if(!sessionStorage.ledgerCurrentId || !sessionStorage.ledgerCurrentName || !sessionStorage.ledgerCurrentTname){
	history.go(-1);
}

if(!sessionStorage.ledgerDetailPnow){
	sessionStorage.ledgerDetailPnow = 1;
}

if(!sessionStorage.ledgerDetailMax){
	sessionStorage.ledgerDetailMax = 2;
}

var infoList;	//台账记录列表容器
var fieldsList;	//台账字段列表容器

var selectArr = [];	//类别字段容器
var searchArr = [];	//搜索字段容器
var selectVals = [];	//类别字段值列表容器

$(function(){
	$('#ledgerName').text(sessionStorage.ledgerCurrentName);	//显示台账名

	
	_getOrCheckField(function(fieldsListRes){		//获取台账的字段列表
		fieldsList = fieldsListRes;					//保存字段列表到容器
		if(fieldsList.length < 1){
			return;
		}
		if(fieldsListRes.length > 0){
			$('#createNewInfoArea').show();			//是否显示添加记录按钮
			$('#fpage').show();
		}

		//筛选搜索和分类字段并保存到容器
		_filterFields(function(){
			_getInfoList();								//获取记录列表
		});	


	});


});


//筛选字段（类别和搜索）
function _filterFields(fun){
	if(fieldsList.length > 0){
		for(var i in fieldsList){
			if(fieldsList[i].isSelect){
				selectArr.push(fieldsList[i]);
			}
			if(fieldsList[i].isSearch){
				searchArr.push(fieldsList[i]);
			}
		}
	}
	_viewSearchSel();
	_getSelectVals(fun);
}

//显示搜索字段
function _viewSearchSel(){
	if(searchArr.length > 0){
		$('#searchArea').show();
		var searchStr = "";
		for(var i=0;i<searchArr.length;i++){
			searchStr += "<option value='"+searchArr[i].fieldTname+"'>"+searchArr[i].fieldName+"</option>";
		}
		$('#searchSel').html(searchStr);

		$('#searchBtn').click(function(){
			var searchInpVal = m_trim($('#searchInp').val());
			sessionStorage.ledgerDetail_searchInpVal = searchInpVal;
			sessionStorage.ledgerDetailPnow = 1;
			sessionStorage.ledgerDetailMax = 2;
			location.reload();
		});

		$('#searchSel').change(function(){
			var searchSelVal = m_trim($('#searchSel').find('option:selected').val());
			sessionStorage.ledgerDetail_searchSelVal = searchSelVal;
		});

		//状态维持
		if(sessionStorage.ledgerDetail_searchSelVal){
			$('#searchSel').val(sessionStorage.ledgerDetail_searchSelVal);
		}
		if(sessionStorage.ledgerDetail_searchInpVal){
			$('#searchInp').val(sessionStorage.ledgerDetail_searchInpVal);
		}
	}
}


//获取分类字段的值列表
function _getSelectVals(fun){
	if(selectArr.length == 1){
		$('#selectArea').show();
		$('#selectName').text(selectArr[0].fieldName+'：');
		var data = {
			type:'_getSelectVals',
			tname:sessionStorage.ledgerCurrentTname,
			lname:selectArr[0].fieldTname
		}
		AjaxBefore(data,function(res){
			var tmp = _checkJsonFormat(res); //检测JSON格式
			_parseSelectVals(tmp.data);
			fun();
		});
	}else{
		fun();
	}
}


//解析分类值
function _parseSelectVals(sdata){
	if(sdata.length < 1){
		return;
	}
	//将分类值保存到数组,用于添加记录时选择分类 
	for(var i=0;i<sdata.length;i++){
		selectVals.push(sdata[i][selectArr[0].fieldTname]);
	}

	var lstr = '<option value="all">全部</option>';
	for(var i=0;i<sdata.length;i++){
		lstr += '<option value="'+sdata[i][selectArr[0].fieldTname]+'">'+sdata[i][selectArr[0].fieldTname]+'</option>';
	}
	$('#selectSel').html(lstr);


	$('#selectSel').change(function(){
		var val = m_trim($(this).find('option:selected').val());
		sessionStorage.ledgerDetail_selectVal = val;
		sessionStorage.ledgerDetailPnow = 1;
		sessionStorage.ledgerDetailMax = 2;
		location.reload();
	});

	//状态维持
	if(sessionStorage.ledgerDetail_selectVal){
		$('#selectSel').val(sessionStorage.ledgerDetail_selectVal);
	}

}


//获取记录列表
function _getInfoList(){
	if(selectArr.length > 0){
		var selectField = selectArr[0].fieldTname;
		var selectFieldVal = m_trim($('#selectSel').find('option:selected').val());	
	}else{
		var selectField = '';	//分类字段名
		var selectFieldVal = '';	//分类字段值
	}
	var data = {
		type:'_getInfoList',
		tname:sessionStorage.ledgerCurrentTname,
		selectField:selectField,
		selectFieldVal:selectFieldVal,
		searchField:m_trim($('#searchSel').find('option:selected').val()),
		searchVal:m_trim($('#searchInp').val()),
		pnow:sessionStorage.ledgerDetailPnow
	}
	AjaxBefore(data,function(res){
		C('_getInfoList after');
		var tmp = _checkJsonFormat(res); //检测JSON格式
		if(tmp.stat == 'err2'){
			$('#tbody > tr > td').text('有非法字符！');
			return;
		}
		_parseInfoData(tmp.data)
	});
}

function _parseInfoData(data){
	infoList = data;	//保存记录到容器
	if(infoList.length < 1){
		$('#thead').html('');
		$('#tbody').html('<tr><td>暂无数据</td></tr>');
		sessionStorage.ledgerDetailMax = sessionStorage.ledgerDetailPnow;
	//	return;
	}else{

	   if(infoList.length < 1){
		sessionStorage.ledgerDetailPnow = parseInt(sessionStorage.ledgerDetailPnow) - 1;
		sessionStorage.ledgerDetailMax = sessionStorage.ledgerDetailPnow;
	   }else{

		//提取并显示表头
		var theadStr = '<tr>';
		for(var i=0;i<fieldsList.length;i++){
			theadStr += "<th>" + fieldsList[i].fieldName + "</th>";
		}
		theadStr += "<th>操作</th>";
		theadStr += '</tr>';
		$('#thead').html(theadStr);


		//展示表主体
		var tbodyStr = '';
		for(var i=0;i<data.length;i++){
			tbodyStr += '<tr>';
			for(var j in data[i]){
				if(j == 'id'){
					continue;
				}else{
					tbodyStr += "<td style='font-size:13px'>" + data[i][j] + "</td>";
				}
			}
			tbodyStr += "<td><span onclick='_changeInfo(" + data[i].id + ")'>修改</span>　<span onclick=_delInfo(" + data[i].id + ")>删除</span></td>";
			tbodyStr += '</tr>';
		}
		$('#tbody').html(tbodyStr);
	   }
	}

	Fpage(sessionStorage.ledgerDetailPnow,sessionStorage.ledgerDetailMax,function(p){
		sessionStorage.ledgerDetailPnow = p;
		sessionStorage.ledgerDetailMax = parseInt(p+1);
		_getInfoList();
	},1,1,'#fpage');
}


//删除记录
function _delInfo(id){
	if(!confirm('确认删除？')){
		return;
	}
	var vcode = Math.round(Math.random()*10000);
	if(vcode != prompt('请输入确认码：'+vcode)){
	        A('确认码不正确！');
	        return;
	}
	var data = {
		type:'_delInfo',
		tname:sessionStorage.ledgerCurrentTname,
		id:id
	}
	AjaxBefore(data,function(res){
		var tmp = _checkJsonFormat(res); //检测JSON格式
		if(tmp.stat == 'ok'){
			A('删除成功！');
		}else{
			A('删除失败！');
		}
		location.reload();
	});
}


//创建新记录
function _createNewInfo(){
	$('#editArea').show();

	if(fieldsList.length < 1){
		$('#addNewInfoBtn').hide();
		$('#addNewInfoArea').html("<h3 style='color:red;text-align:center'>请先设计台账</h3>");
	}

	
	var tmp_str = '';
	for(var i=0;i<fieldsList.length;i++){
		if(fieldsList[i].isArea){
			tmp_str += "<div style='margin:10px auto;border:0px solid blue'><div style='float:left;width:25%;border:0px solid;text-align:right'>" + fieldsList[i].fieldName + "</div>　<div style='float:right;width:70%;border:0px solid'><textarea name='" + fieldsList[i].fieldTname + "'style='width:200px;height:150px;resize:none'></textarea></div><div style='clear:both'></div></div>";
		}else{
			if(fieldsList[i].isSelect){
				var radioStr = '<span>';
				for(var s=0;s<selectVals.length;s++){
					radioStr += "<label><input type='radio' name='selectVal' value='"+selectVals[s]+"'/>"+selectVals[s]+"</label>　";
				}
				radioStr += '</span>';
				tmp_str += "<div style='margin:10px auto;border:0px solid blue'><div style='float:left;width:25%;border:0px solid;text-align:right'>" + fieldsList[i].fieldName + "</div>　<div style='float:right;width:70%;border:0px solid'><input type='text' name='" + fieldsList[i].fieldTname + "' style='width:50px' />"+radioStr+"</div><div style='clear:both'></div></div>";
			}else{
				tmp_str += "<div style='margin:10px auto;border:0px solid blue'><div style='float:left;width:25%;border:0px solid;text-align:right'>" + fieldsList[i].fieldName + "</div>　<div style='float:right;width:70%;border:0px solid'><input type='text' name='" + fieldsList[i].fieldTname + "' style='width:200px' /></div><div style='clear:both'></div></div>";
			}
		}
	}
	$('#addNewInfoArea').html(tmp_str);

	$('input[name=selectVal]').change(function(){
		var selectVal = $(this).val();
		$(this).parent().parent().prev().val(selectVal);
	});



	//添加新记录提交
	$('#addNewInfoBtn').click(function(){
		var data2 = $("#f1").serializeArray();

		var data = {
			type:'addNewInfo',
			tname:sessionStorage.ledgerCurrentTname
		};
		for(var i=0;i<data2.length;i++){
			if(!m_trim(data2[i].value)){
				A('数据项不得为空！');
				return;
			}
			if(data2[i].name == 'selectVal'){
				continue;
			}
			data[data2[i].name] = m_trim(data2[i].value);
			if(!_checkChar(data[data2[i].name])){
				A('非法字符！');
				return;
			}
		}
		Notice('数据处理中......');
		AjaxBefore(data,function(res){
			Notice();
			var tmp = _checkJsonFormat(res); //检测JSON格式
			if(tmp.stat == 'ok'){
				A('添加成功！');
				sessionStorage.ledgerDetailPnow = 1;
				sessionStorage.ledgerDetailMax = 2;
			}else{
				A('添加失败！');
			}
			location.reload();
		});
	});
}




//修改返回
function _editBack(){
	$('#editArea').hide();
}



//修改信息
function _changeInfo(id){
	var tmp_info = '';
	for(var i=0;i<infoList.length;i++){
		if(infoList[i].id == id){
			tmp_info = infoList[i];
		}
	}
	if(!tmp_info){
		return;
	}

	$('#addInfoTitle').text('修改记录');

	$('#editArea').show();
	
	if(fieldsList.length < 1){
		$('#addNewInfoBtn').hide();
		$('#addNewInfoArea').html("<h3 style='color:red;text-align:center'>请先设计台账</h3>");
	}
	var tmp_str = '';
	for(var i=0;i<fieldsList.length;i++){
		if(fieldsList[i].isArea){
			tmp_str += "<div style='margin:10px auto;border:0px solid blue'><div style='float:left;width:25%;border:0px solid;text-align:right'>" + fieldsList[i].fieldName + "</div>　<div style='float:right;width:70%;border:0px solid'><textarea name='" + fieldsList[i].fieldTname + "' style='width:200px;height:150px;resize:none'>"+tmp_info[fieldsList[i].fieldTname]+"</textarea></div><div style='clear:both'></div></div>";
		}else{
			tmp_str += "<div style='margin:10px auto;border:0px solid blue'><div style='float:left;width:25%;border:0px solid;text-align:right'>" + fieldsList[i].fieldName + "</div>　<div style='float:right;width:70%;border:0px solid'><input type='text' name='" + fieldsList[i].fieldTname + "' value='"+tmp_info[fieldsList[i].fieldTname]+"' style='width:200px' /></div><div style='clear:both'></div></div>";
		}
	}
	$('#addNewInfoArea').html(tmp_str);



	//添加新记录提交
	$('#addNewInfoBtn').click(function(){
		var data2 = $("#f1").serializeArray();
		var data = {
			type:'updateInfo',
			tname:sessionStorage.ledgerCurrentTname,
			id:id
		};
		for(var i=0;i<data2.length;i++){
			if(!data2[i].value){
				A('数据项不得为空！');
				return;
			}
			data[data2[i].name] = m_trim(data2[i].value);
			if(!_checkChar(data[data2[i].name])){
				A('非法字符！');
				return;
			}
		}
		AjaxBefore(data,function(res){
			var tmp = _checkJsonFormat(res); //检测JSON格式
			if(tmp.stat == 'ok'){
				A('修改成功！');
			}else{
				A('修改失败！');
			}
			location.reload();
		});
	});
}





//清除相关缓存
function _clearSess(){
	sessionStorage.ledgerDetail_selectVal = '';	//清除类别缓存
	sessionStorage.ledgerDetail_searchSelVal = '';
	sessionStorage.ledgerDetail_searchInpVal = '';
	sessionStorage.ledgerDetailPnow = 1;
	sessionStorage.ledgerDetailMax = 2;
}

//返回
function _backGo(){
	_clearSess();
	history.go(-1);
}

//跳转到台账设计
function _jumpDesign(){
	_clearSess();
	location.href="ledger_design.html";
}








</script>

